package com.sleep.psy.external_server.business.psy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sleep.psy.external_server.business.psy.entity.PsyBaseVideo;
import com.sleep.psy.external_server.business.psy.mapper.PsyBaseVideoMapper;
import com.sleep.psy.external_server.business.psy.service.PsyBaseVideoService;
import com.sleep.psy.external_server.constants.MySqlConstant;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 基础视频列表 服务实现类
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Service
public class PsyBaseVideoServiceImpl extends ServiceImpl<PsyBaseVideoMapper, PsyBaseVideo> implements PsyBaseVideoService {

    @Autowired
    private PsyBaseVideoMapper psyBaseVideoMapper;

    /**
     * 通过业务分类获取有效的基础视频
     * @author wenliguo
     * @date 2025/6/9 11:13
     * @param bsiType 业务分类
     * @return java.util.List<com.sleep.psy.main_server.business2.evaluation.entity.PsyBaseVideo>
     */
    @Override
    public List<PsyBaseVideo> selectBaseVideoByBsiType(Integer bsiType) {
        QueryWrapper<PsyBaseVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("bsi_type", bsiType)
                .eq("del_flag", MySqlConstant.PSY_BASE_VIDEO_DEL_FLAG_0);
        return psyBaseVideoMapper.selectList(wrapper);
    }

    /**
     * 通过业务分类和序号获取每日一课
     * @author wenliguo
     * @date 2025/6/11 14:06
     * @param bsiType 业务id
     * @param audioSort 排序
     * @return com.sleep.psy.main_server.business2.evaluation.entity.PsyBaseVideo
     */
    @Override
    public PsyBaseVideo selectVideByBsiTypeAndSort(Integer bsiType, Integer audioSort) {
        QueryWrapper<PsyBaseVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("bsi_type", bsiType)
                .eq("del_flag", MySqlConstant.PSY_BASE_VIDEO_DEL_FLAG_0)
                .eq("audio_sort", audioSort);
        return psyBaseVideoMapper.selectOne(wrapper);
    }

    /**
     * 通过id获取有效的基础视频
     * @author wenliguo
     * @date 2025/8/13 10:57
     * @param id id
     * @return com.sleep.psy.external_server.psy.entity.PsyBaseVideo
     */
    @Override
    public PsyBaseVideo selectById(Integer id) {
        QueryWrapper<PsyBaseVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id)
                .eq("del_flag", MySqlConstant.PSY_BASE_VIDEO_DEL_FLAG_0);
        return psyBaseVideoMapper.selectOne(wrapper);
    }

    /**
     * 通过多个id查询
     * @author wenliguo
     * @date 2025/8/13 15:56
     * @param ids ids
     * @return java.util.List<com.sleep.psy.external_server.psy.entity.PsyBaseVideo>
     */
    @Override
    public List<PsyBaseVideo> selectByIds(List<String> ids) {

        QueryWrapper<PsyBaseVideo> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids)
                .eq("del_flag", MySqlConstant.PSY_BASE_VIDEO_DEL_FLAG_0);
        return psyBaseVideoMapper.selectList(wrapper);
    }

    /**
     * 获取多个音频
     * @author wenliguo
     * @date 2025/8/17 13:32
     * @param ids ids
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectVideoByIds(String ids) {
        List<String> list = new ArrayList<>();
        if (ids.contains(",")) {
            String[] split = ids.split(",");
            list.addAll(Arrays.asList(split));
        } else {
            list.add(ids);
        }
        QueryWrapper<PsyBaseVideo> wrapper = new QueryWrapper<>();
        wrapper.in("id", list)
                .eq("del_flag", MySqlConstant.PSY_BASE_VIDEO_DEL_FLAG_0);
        return ResultUtil.success(psyBaseVideoMapper.selectList(wrapper));

    }

    /**
     * 获取通用的训练
     * @author wenliguo
     * @date 2025/8/20 11:34
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectGenernal() {
        return ResultUtil.success(selectBaseVideoByBsiType(MySqlConstant.BSI_TYPE_4));
    }


    /**
     * 训练课程列表
     * @author wenliguo
     * @date 2025/8/21 16:27
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectTraingList() {
        QueryWrapper<PsyBaseVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", MySqlConstant.PSY_BASE_VIDEO_DEL_FLAG_0)
                .in("bsi_type",Arrays.asList(1, 2, 3));
        return ResultUtil.success(psyBaseVideoMapper.selectList(wrapper));
    }

    /**
     * 冥想专项
     * @author wenliguo
     * @date 2025/8/21 16:28
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectMeditationList() {
        return ResultUtil.success(selectBaseVideoByBsiType(MySqlConstant.BSI_TYPE_5));
    }

    /**
     * 睡眠训练
     * @author wenliguo
     * @date 2025/8/21 16:28
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectSleepList() {
        return ResultUtil.success(selectBaseVideoByBsiType(MySqlConstant.BSI_TYPE_3));
    }

    /**
     * 放松减压
     * @author wenliguo
     * @date 2025/8/21 16:28
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectRelaxList() {
        return ResultUtil.success(selectBaseVideoByBsiType(MySqlConstant.BSI_TYPE_6));
    }
}
